package cn.hd.controller;

import cn.hd.entity.Bill;
import cn.hd.entity.Result;
import cn.hd.entity.User;

import cn.hd.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;


@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/addUser")
    public Object addUser(User user){
        user.setRole(2);
        int i = userService.addUser(user);
        System.out.println(user.toString());
        if(i == 1){
            return new Result("200","注册成功",null);
        }else{
            return new Result("400","注册失败",null);
        }
    }


    @RequestMapping("/delUser")
    public Object delUser(long id){
        int i = userService.delUser(id);
        if(i == 1){
            return new Result("200","删除成功",null);
        }else{
            return new Result("400","删除失败",null);
        }
    }

    @RequestMapping("/updateUser")
    public Object updateUser(User user){
        int i = userService.updateUser(user);
        if(i == 1){
            return new Result("200","修改成功",null);
        }else{
            return new Result("400","修改失败",null);
        }
    }

    @RequestMapping("/selectUsers")
    public Object selectAllUser(int pagenum,int pagesize,String username){
        Map map = new HashMap();
        map.put("username",username);
        PageInfo<User> userPageInfo = userService.selectAllUser(pagenum,pagesize,map);
        if(userPageInfo.getList().size() != 0){
            return new Result("200","查询成功",userPageInfo);
        }else{
            return new Result("400","查询失败",null);
        }
    }

    @RequestMapping("/selectUserById")
    public Object selectUserById(long id){
        User user = userService.selectUserById(id);
        Calendar cd = Calendar.getInstance();
        cd.setTime(user.getBirthday());
        cd.add(Calendar.DATE,1);
        user.setBirthday(cd.getTime());
        if(user != null){
            return new Result("200","查询成功",user);
        }else{
            return new Result("400","查询失败",null);
        }
    }

    @RequestMapping("/login")
    public Object login(User user){
        User us = userService.selectUserByName(user.getUsername());
        if(us != null){
            if(user.getPassword().equals(us.getPassword())){
                return new Result("200","登录成功",us);
            }else{
                return new Result("400","密码错误",user);
            }
        }else{
            return new Result("400","查询失败",null);
        }
    }


//处理没有登陆的响应信息
    @RequestMapping("/tologin")
    public Object tologin(){
        return new Result("401","没有登陆，请登录!!!",null);
    }



    //zjc--开始
    @RequestMapping("/updateUser2")
    public Object updateUser2(User user){
        int i = userService.updateUser2(user);
        if(i != 0){
            return new Result("200","修改成功",null);
        }else{
            return new Result("400","修改失败",null);
        }
    }

    @RequestMapping("/updateUserPassword")
    public Object updateUserPassword(String oldpassword,String newpassword, String qrpassowrd,Long uid){
        User user = userService.selectUserById(uid);
        if (!user.getPassword().equals(oldpassword)){
            return new Result("400","旧密码错误",null);
        }else {
            if (!newpassword.equals(qrpassowrd)){
                return new Result("400","两次密码不一致",null);
            }else {
                userService.updateUserPassword(newpassword,uid);
                return new Result("200","修改成功",null);
            }

        }
    }
    @RequestMapping("/selectzj")
    public Object selectzj(Integer uid){
        Map map = userService.selectzj(uid);
        return new Result("200","查询成功",map);
    }


}
